#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "Log.hpp"
#include"Common.hpp"

using namespace std;
using namespace Log_M;

const string gpath = "./";
const string gfilename = "dict.txt";
const string gsep = ": ";

class Dictionary
{
private:
    unordered_map<string, string> _dictionary;
    string _path;
    string _filename;

private:
    // 加载字典
    // 从文件中读取
    bool Load_Dictionary()
    {
        string file = _path + _filename;
        ifstream in;
        in.open(file.c_str());
        if (!in.is_open())
        {
            Log(Log_Level::ERROR) << "open file fail!";
            return false;
        }
        // 读取字符
        //  char ch;
        //  while (in.get(ch))
        //  {
        //      std::cout << ch;
        //  }
        // 读取一行
        string line;
        while (getline(in, line))
        {
            //对行的字符串做分割
            string key;
            string value;
            bool is=separate(line,&key,&value,gsep);
            if(is)
            {
                _dictionary.insert(make_pair(key,value));
            }
        }
        in.close();
        return true;
    }

public:
    Dictionary(const string &path = gpath, const string &filename = gfilename)
        : _path(path),
          _filename(filename)
    {
        Load_Dictionary();
        //测试
        //print();
    }

    string Translate(const string & word)
    {
        auto item=_dictionary.find(word);
        if(item==_dictionary.end()) return "None";
        return item->second; 
    }

    void print()
    {
        for(auto&e:_dictionary)
        {
            cout<<e.first<<": "<<e.second<<endl;
        }
    }

    ~Dictionary()
    {
    }
};